/*!
 * \file test_wt_uft_engine.cpp
 * \project WonderTrader
 *
 * \author AI Assistant
 * \date 2024/12/01
 * 
 * \brief WtUftEngine及相关UFT模块的综合测试文件
 * 
 * \details 本文件包含对WtUftEngine（超高频交易引擎）及相关UFT模块的全面测试，
 *          涵盖引擎初始化、数据管理、策略上下文、实时行情处理等核心功能。
 *          测试用例包括功能测试、性能测试、边界条件测试和并发测试。
 */

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <memory>
#include <thread>
#include <chrono>
#include <atomic>
#include <vector>
#include <string>

// 模拟UFT相关头文件和类
namespace {

// 模拟WtUftEngine类
class MockWtUftEngine {
public:
    MockWtUftEngine() : _cur_date(20241201), _cur_time(93000), _cur_secs(0), _cur_tdate(20241201) {}
    virtual ~MockWtUftEngine() = default;

    // 时间管理接口
    void set_date_time(uint32_t curDate, uint32_t curTime, uint32_t curSecs = 0, uint32_t rawTime = 0) {
        _cur_date = curDate;
        _cur_time = curTime;
        _cur_secs = curSecs;
        _cur_raw_time = (rawTime == 0) ? curTime : rawTime;
    }

    void set_trading_date(uint32_t curTDate) { _cur_tdate = curTDate; }
    
    uint32_t get_date() const { return _cur_date; }
    uint32_t get_min_time() const { return _cur_time; }
    uint32_t get_raw_time() const { return _cur_raw_time; }
    uint32_t get_secs() const { return _cur_secs; }
    uint32_t get_trading_date() const { return _cur_tdate; }

    // 数据管理接口
    double get_cur_price(const char* stdCode) {
        if (strcmp(stdCode, "SHFE.rb2501") == 0) return 3850.0;
        if (strcmp(stdCode, "CFFEX.IF2412") == 0) return 4200.0;
        return 0.0;
    }

    // 策略管理接口
    void addContext(uint32_t id) { _contexts[id] = true; }
    bool hasContext(uint32_t id) const { return _contexts.find(id) != _contexts.end(); }
    size_t getContextCount() const { return _contexts.size(); }

    // 订阅管理接口
    void sub_tick(uint32_t sid, const char* stdCode) {
        _tick_subs[stdCode].insert(sid);
    }
    
    void sub_order_queue(uint32_t sid, const char* stdCode) {
        _ordque_subs[stdCode].insert(sid);
    }
    
    void sub_order_detail(uint32_t sid, const char* stdCode) {
        _orddtl_subs[stdCode].insert(sid);
    }
    
    void sub_transaction(uint32_t sid, const char* stdCode) {
        _trans_subs[stdCode].insert(sid);
    }

    // 获取订阅信息
    size_t getTickSubCount(const char* stdCode) const {
        auto it = _tick_subs.find(stdCode);
        return (it != _tick_subs.end()) ? it->second.size() : 0;
    }

    // 模拟数据处理
    void on_tick(const char* stdCode, double price, uint32_t volume) {
        _last_tick_data[stdCode] = {price, volume, std::chrono::high_resolution_clock::now()};
        _tick_count++;
    }

    void on_session_begin() { _session_active = true; }
    void on_session_end() { _session_active = false; }
    bool is_session_active() const { return _session_active; }

    // 性能统计
    uint64_t get_tick_count() const { return _tick_count; }
    void reset_stats() { _tick_count = 0; }

private:
    uint32_t _cur_date, _cur_time, _cur_secs, _cur_raw_time, _cur_tdate;
    std::map<uint32_t, bool> _contexts;
    std::map<std::string, std::set<uint32_t>> _tick_subs, _ordque_subs, _orddtl_subs, _trans_subs;
    
    struct TickData {
        double price;
        uint32_t volume;
        std::chrono::high_resolution_clock::time_point timestamp;
    };
    std::map<std::string, TickData> _last_tick_data;
    std::atomic<uint64_t> _tick_count{0};
    bool _session_active = false;
};

// 模拟WtUftDtMgr类
class MockWtUftDtMgr {
public:
    MockWtUftDtMgr() = default;
    virtual ~MockWtUftDtMgr() = default;

    bool init(const std::string& config) {
        _initialized = true;
        _config = config;
        return true;
    }

    void handle_push_quote(const char* stdCode, double price, uint32_t volume) {
        _quote_data[stdCode] = {price, volume};
        _quote_count++;
    }

    bool is_initialized() const { return _initialized; }
    uint64_t get_quote_count() const { return _quote_count; }
    
    bool has_quote(const char* stdCode) const {
        return _quote_data.find(stdCode) != _quote_data.end();
    }

private:
    bool _initialized = false;
    std::string _config;
    struct QuoteData { double price; uint32_t volume; };
    std::map<std::string, QuoteData> _quote_data;
    std::atomic<uint64_t> _quote_count{0};
};

// 模拟UftStraBaseCtx类
class MockUftStraBaseCtx {
public:
    MockUftStraBaseCtx(uint32_t id, const std::string& name) : _id(id), _name(name) {}
    virtual ~MockUftStraBaseCtx() = default;

    uint32_t id() const { return _id; }
    const std::string& name() const { return _name; }

    virtual void on_init() { _initialized = true; }
    virtual void on_tick(const char* stdCode, double price) {
        _tick_received[stdCode] = price;
        _tick_count++;
    }
    virtual void on_session_begin(uint32_t trading_date) { _session_date = trading_date; }
    virtual void on_session_end(uint32_t trading_date) { _session_date = 0; }

    bool is_initialized() const { return _initialized; }
    uint64_t get_tick_count() const { return _tick_count; }
    uint32_t get_session_date() const { return _session_date; }

private:
    uint32_t _id;
    std::string _name;
    bool _initialized = false;
    std::map<std::string, double> _tick_received;
    std::atomic<uint64_t> _tick_count{0};
    uint32_t _session_date = 0;
};

} // anonymous namespace

/*!
 * \class WtUftEngineTest
 * \brief WtUftEngine基础功能测试类
 * 
 * \details 测试WtUftEngine的基本功能，包括初始化、时间管理、数据处理等
 */
class WtUftEngineTest : public ::testing::Test {
protected:
    void SetUp() override {
        engine = std::make_unique<MockWtUftEngine>();
        data_mgr = std::make_unique<MockWtUftDtMgr>();
    }

    void TearDown() override {
        engine.reset();
        data_mgr.reset();
    }

    std::unique_ptr<MockWtUftEngine> engine;
    std::unique_ptr<MockWtUftDtMgr> data_mgr;
};

/*!
 * \brief 测试WtUftEngine基本功能
 * 
 * \details 验证引擎的基本初始化和状态管理功能
 */
TEST_F(WtUftEngineTest, BasicFunctionality) {
    // 测试初始状态
    EXPECT_EQ(engine->get_date(), 20241201U) << "引擎初始日期设置错误";
    EXPECT_EQ(engine->get_min_time(), 93000U) << "引擎初始时间设置错误";
    EXPECT_EQ(engine->get_trading_date(), 20241201U) << "引擎初始交易日设置错误";
    
    // 测试时间设置
    engine->set_date_time(20241202, 100000, 30, 100030);
    EXPECT_EQ(engine->get_date(), 20241202U) << "日期设置失败";
    EXPECT_EQ(engine->get_min_time(), 100000U) << "时间设置失败";
    EXPECT_EQ(engine->get_secs(), 30U) << "秒数设置失败";
    EXPECT_EQ(engine->get_raw_time(), 100030U) << "原始时间设置失败";
    
    // 测试交易日设置
    engine->set_trading_date(20241203);
    EXPECT_EQ(engine->get_trading_date(), 20241203U) << "交易日设置失败";
    
    std::cout << "WtUftEngine基本功能测试通过" << std::endl;
}

/*!
 * \brief 测试价格查询功能
 * 
 * \details 验证引擎的价格查询和数据管理功能
 */
TEST_F(WtUftEngineTest, PriceQuery) {
    // 测试已知合约价格查询
    double rb_price = engine->get_cur_price("SHFE.rb2501");
    EXPECT_DOUBLE_EQ(rb_price, 3850.0) << "螺纹钢价格查询错误";
    
    double if_price = engine->get_cur_price("CFFEX.IF2412");
    EXPECT_DOUBLE_EQ(if_price, 4200.0) << "股指期货价格查询错误";
    
    // 测试未知合约价格查询
    double unknown_price = engine->get_cur_price("UNKNOWN.CONTRACT");
    EXPECT_DOUBLE_EQ(unknown_price, 0.0) << "未知合约应返回0价格";
    
    std::cout << "价格查询功能测试通过，查询了3个合约" << std::endl;
}

/*!
 * \brief 测试策略上下文管理
 * 
 * \details 验证引擎的策略上下文添加和管理功能
 */
TEST_F(WtUftEngineTest, ContextManagement) {
    // 测试初始状态
    EXPECT_EQ(engine->getContextCount(), 0U) << "初始上下文数量应为0";
    
    // 添加策略上下文
    engine->addContext(1001);
    engine->addContext(1002);
    engine->addContext(1003);
    
    EXPECT_EQ(engine->getContextCount(), 3U) << "添加3个上下文后数量错误";
    EXPECT_TRUE(engine->hasContext(1001)) << "上下文1001应存在";
    EXPECT_TRUE(engine->hasContext(1002)) << "上下文1002应存在";
    EXPECT_TRUE(engine->hasContext(1003)) << "上下文1003应存在";
    EXPECT_FALSE(engine->hasContext(1004)) << "上下文1004不应存在";
    
    std::cout << "策略上下文管理测试通过，管理了3个上下文" << std::endl;
}

/*!
 * \brief 测试数据订阅功能
 * 
 * \details 验证引擎的数据订阅和分发功能
 */
TEST_F(WtUftEngineTest, DataSubscription) {
    const char* contract1 = "SHFE.rb2501";
    const char* contract2 = "CFFEX.IF2412";
    
    // 测试Tick数据订阅
    engine->sub_tick(1001, contract1);
    engine->sub_tick(1002, contract1);
    engine->sub_tick(1001, contract2);
    
    EXPECT_EQ(engine->getTickSubCount(contract1), 2U) << "合约1的Tick订阅数量错误";
    EXPECT_EQ(engine->getTickSubCount(contract2), 1U) << "合约2的Tick订阅数量错误";
    
    // 测试其他数据类型订阅
    engine->sub_order_queue(1001, contract1);
    engine->sub_order_detail(1002, contract1);
    engine->sub_transaction(1003, contract1);
    
    std::cout << "数据订阅功能测试通过，订阅了多种数据类型" << std::endl;
}

/*!
 * \brief 测试交易时段管理
 * 
 * \details 验证引擎的交易时段开始和结束处理
 */
TEST_F(WtUftEngineTest, SessionManagement) {
    // 测试初始状态
    EXPECT_FALSE(engine->is_session_active()) << "初始状态交易时段应未激活";
    
    // 测试交易时段开始
    engine->on_session_begin();
    EXPECT_TRUE(engine->is_session_active()) << "交易时段开始后应激活";
    
    // 测试交易时段结束
    engine->on_session_end();
    EXPECT_FALSE(engine->is_session_active()) << "交易时段结束后应未激活";
    
    std::cout << "交易时段管理测试通过" << std::endl;
}

/*!
 * \class WtUftDataManagerTest
 * \brief WtUftDtMgr数据管理器测试类
 * 
 * \details 测试UFT数据管理器的数据处理和管理功能
 */
class WtUftDataManagerTest : public ::testing::Test {
protected:
    void SetUp() override {
        data_mgr = std::make_unique<MockWtUftDtMgr>();
    }

    void TearDown() override {
        data_mgr.reset();
    }

    std::unique_ptr<MockWtUftDtMgr> data_mgr;
};

/*!
 * \brief 测试数据管理器初始化
 * 
 * \details 验证数据管理器的初始化功能
 */
TEST_F(WtUftDataManagerTest, Initialization) {
    // 测试初始状态
    EXPECT_FALSE(data_mgr->is_initialized()) << "初始状态应未初始化";
    
    // 测试初始化
    bool result = data_mgr->init("test_config");
    EXPECT_TRUE(result) << "初始化应成功";
    EXPECT_TRUE(data_mgr->is_initialized()) << "初始化后状态应为已初始化";
    
    std::cout << "数据管理器初始化测试通过" << std::endl;
}

/*!
 * \brief 测试行情数据处理
 * 
 * \details 验证数据管理器的行情数据推送处理功能
 */
TEST_F(WtUftDataManagerTest, QuoteHandling) {
    data_mgr->init("test_config");
    
    // 测试初始状态
    EXPECT_EQ(data_mgr->get_quote_count(), 0U) << "初始行情数量应为0";
    
    // 推送行情数据
    data_mgr->handle_push_quote("SHFE.rb2501", 3850.5, 1000);
    data_mgr->handle_push_quote("CFFEX.IF2412", 4200.0, 500);
    data_mgr->handle_push_quote("SHFE.rb2501", 3851.0, 800);
    
    EXPECT_EQ(data_mgr->get_quote_count(), 3U) << "推送3条行情后数量错误";
    EXPECT_TRUE(data_mgr->has_quote("SHFE.rb2501")) << "应包含螺纹钢行情";
    EXPECT_TRUE(data_mgr->has_quote("CFFEX.IF2412")) << "应包含股指期货行情";
    
    std::cout << "行情数据处理测试通过，处理了3条行情" << std::endl;
}

/*!
 * \class UftStraContextTest
 * \brief UFT策略上下文测试类
 * 
 * \details 测试UFT策略上下文的功能和生命周期管理
 */
class UftStraContextTest : public ::testing::Test {
protected:
    void SetUp() override {
        context1 = std::make_unique<MockUftStraBaseCtx>(2001, "TestStrategy1");
        context2 = std::make_unique<MockUftStraBaseCtx>(2002, "TestStrategy2");
    }

    void TearDown() override {
        context1.reset();
        context2.reset();
    }

    std::unique_ptr<MockUftStraBaseCtx> context1;
    std::unique_ptr<MockUftStraBaseCtx> context2;
};

/*!
 * \brief 测试策略上下文基本属性
 * 
 * \details 验证策略上下文的ID和名称等基本属性
 */
TEST_F(UftStraContextTest, BasicProperties) {
    EXPECT_EQ(context1->id(), 2001U) << "策略1的ID错误";
    EXPECT_EQ(context1->name(), "TestStrategy1") << "策略1的名称错误";
    
    EXPECT_EQ(context2->id(), 2002U) << "策略2的ID错误";
    EXPECT_EQ(context2->name(), "TestStrategy2") << "策略2的名称错误";
    
    std::cout << "策略上下文基本属性测试通过" << std::endl;
}

/*!
 * \brief 测试策略初始化
 * 
 * \details 验证策略上下文的初始化功能
 */
TEST_F(UftStraContextTest, Initialization) {
    // 测试初始状态
    EXPECT_FALSE(context1->is_initialized()) << "初始状态应未初始化";
    
    // 测试初始化
    context1->on_init();
    EXPECT_TRUE(context1->is_initialized()) << "初始化后应为已初始化状态";
    
    std::cout << "策略初始化测试通过" << std::endl;
}

/*!
 * \brief 测试Tick数据处理
 * 
 * \details 验证策略上下文的Tick数据接收和处理功能
 */
TEST_F(UftStraContextTest, TickProcessing) {
    context1->on_init();
    
    // 测试初始状态
    EXPECT_EQ(context1->get_tick_count(), 0U) << "初始Tick数量应为0";
    
    // 发送Tick数据
    context1->on_tick("SHFE.rb2501", 3850.0);
    context1->on_tick("CFFEX.IF2412", 4200.0);
    context1->on_tick("SHFE.rb2501", 3851.0);
    
    EXPECT_EQ(context1->get_tick_count(), 3U) << "接收3个Tick后数量错误";
    
    std::cout << "Tick数据处理测试通过，处理了3个Tick" << std::endl;
}

/*!
 * \brief 测试交易时段回调
 * 
 * \details 验证策略上下文的交易时段开始和结束回调
 */
TEST_F(UftStraContextTest, SessionCallbacks) {
    // 测试交易时段开始
    context1->on_session_begin(20241201);
    EXPECT_EQ(context1->get_session_date(), 20241201U) << "交易时段开始日期错误";
    
    // 测试交易时段结束
    context1->on_session_end(20241201);
    EXPECT_EQ(context1->get_session_date(), 0U) << "交易时段结束后日期应重置";
    
    std::cout << "交易时段回调测试通过" << std::endl;
}

/*!
 * \class UftPerformanceTest
 * \brief UFT性能测试类
 * 
 * \details 测试UFT引擎在高频场景下的性能表现
 */
class UftPerformanceTest : public ::testing::Test {
protected:
    void SetUp() override {
        engine = std::make_unique<MockWtUftEngine>();
        data_mgr = std::make_unique<MockWtUftDtMgr>();
        data_mgr->init("performance_test_config");
    }

    void TearDown() override {
        engine.reset();
        data_mgr.reset();
    }

    std::unique_ptr<MockWtUftEngine> engine;
    std::unique_ptr<MockWtUftDtMgr> data_mgr;
};

/*!
 * \brief 测试高频Tick数据处理性能
 * 
 * \details 验证引擎处理大量Tick数据的性能
 */
TEST_F(UftPerformanceTest, HighFrequencyTickProcessing) {
    const int tick_count = 100000;
    const char* contracts[] = {"SHFE.rb2501", "CFFEX.IF2412", "DCE.i2501", "CZCE.MA501"};
    const int contract_count = sizeof(contracts) / sizeof(contracts[0]);
    
    engine->reset_stats();
    auto start_time = std::chrono::high_resolution_clock::now();
    
    // 模拟高频Tick数据处理
    for (int i = 0; i < tick_count; ++i) {
        const char* contract = contracts[i % contract_count];
        double price = 3850.0 + (i % 100) * 0.1;
        uint32_t volume = 100 + (i % 50);
        
        engine->on_tick(contract, price, volume);
        data_mgr->handle_push_quote(contract, price, volume);
    }
    
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
    
    EXPECT_EQ(engine->get_tick_count(), tick_count) << "处理的Tick数量错误";
    EXPECT_EQ(data_mgr->get_quote_count(), tick_count) << "数据管理器处理的行情数量错误";
    EXPECT_LT(duration.count(), 500000) << "处理10万个Tick应在500毫秒内完成";
    
    double ticks_per_second = (double)tick_count / duration.count() * 1000000;
    std::cout << "高频Tick处理性能测试通过:" << std::endl;
    std::cout << "  处理Tick数量: " << tick_count << std::endl;
    std::cout << "  总耗时: " << duration.count() << " 微秒" << std::endl;
    std::cout << "  处理速度: " << (int)ticks_per_second << " Tick/秒" << std::endl;
}

/*!
 * \brief 测试多策略并发处理性能
 * 
 * \details 验证引擎同时处理多个策略的性能
 */
TEST_F(UftPerformanceTest, MultiStrategyPerformance) {
    const int strategy_count = 50;
    const int operations_per_strategy = 1000;
    
    // 添加多个策略上下文
    for (int i = 0; i < strategy_count; ++i) {
        engine->addContext(3000 + i);
    }
    
    auto start_time = std::chrono::high_resolution_clock::now();
    
    // 模拟多策略并发操作
    std::vector<std::thread> threads;
    std::atomic<int> total_operations{0};
    
    for (int i = 0; i < strategy_count; ++i) {
        threads.emplace_back([&, i]() {
            for (int j = 0; j < operations_per_strategy; ++j) {
                engine->sub_tick(3000 + i, "SHFE.rb2501");
                engine->get_cur_price("SHFE.rb2501");
                total_operations++;
            }
        });
    }
    
    // 等待所有线程完成
    for (auto& thread : threads) {
        thread.join();
    }
    
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
    
    EXPECT_EQ(engine->getContextCount(), strategy_count) << "策略数量错误";
    EXPECT_EQ(total_operations.load(), strategy_count * operations_per_strategy) << "总操作数错误";
    EXPECT_LT(duration.count(), 1000) << "多策略并发处理应在1秒内完成";
    
    std::cout << "多策略并发性能测试通过:" << std::endl;
    std::cout << "  策略数量: " << strategy_count << std::endl;
    std::cout << "  每策略操作数: " << operations_per_strategy << std::endl;
    std::cout << "  总操作数: " << total_operations.load() << std::endl;
    std::cout << "  总耗时: " << duration.count() << " 毫秒" << std::endl;
}

/*!
 * \class UftIntegrationTest
 * \brief UFT集成测试类
 * 
 * \details 测试UFT各模块之间的集成和协调工作
 */
class UftIntegrationTest : public ::testing::Test {
protected:
    void SetUp() override {
        engine = std::make_unique<MockWtUftEngine>();
        data_mgr = std::make_unique<MockWtUftDtMgr>();
        
        // 初始化数据管理器
        data_mgr->init("integration_test_config");
        
        // 创建策略上下文
        for (int i = 0; i < 3; ++i) {
            contexts.push_back(std::make_unique<MockUftStraBaseCtx>(4000 + i, "IntegrationStrategy" + std::to_string(i)));
            contexts.back()->on_init();
            engine->addContext(4000 + i);
        }
    }

    void TearDown() override {
        contexts.clear();
        engine.reset();
        data_mgr.reset();
    }

    std::unique_ptr<MockWtUftEngine> engine;
    std::unique_ptr<MockWtUftDtMgr> data_mgr;
    std::vector<std::unique_ptr<MockUftStraBaseCtx>> contexts;
};

/*!
 * \brief 测试完整的数据流处理
 * 
 * \details 验证从行情接收到策略处理的完整数据流
 */
TEST_F(UftIntegrationTest, CompleteDataFlow) {
    const char* contract = "SHFE.rb2501";
    
    // 订阅数据
    for (size_t i = 0; i < contexts.size(); ++i) {
        engine->sub_tick(4000 + i, contract);
    }
    
    // 模拟行情数据流
    std::vector<std::pair<double, uint32_t>> market_data = {
        {3850.0, 1000}, {3850.5, 800}, {3851.0, 1200}, {3850.5, 900}, {3852.0, 1500}
    };
    
    for (const auto& data : market_data) {
        // 引擎接收行情
        engine->on_tick(contract, data.first, data.second);
        
        // 数据管理器处理行情
        data_mgr->handle_push_quote(contract, data.first, data.second);
        
        // 策略接收行情
        for (auto& context : contexts) {
            context->on_tick(contract, data.first);
        }
    }
    
    // 验证数据流处理结果
    EXPECT_EQ(engine->get_tick_count(), market_data.size()) << "引擎处理的Tick数量错误";
    EXPECT_EQ(data_mgr->get_quote_count(), market_data.size()) << "数据管理器处理的行情数量错误";
    
    for (const auto& context : contexts) {
        EXPECT_EQ(context->get_tick_count(), market_data.size()) << "策略接收的Tick数量错误";
    }
    
    std::cout << "完整数据流处理测试通过:" << std::endl;
    std::cout << "  处理行情数: " << market_data.size() << std::endl;
    std::cout << "  参与策略数: " << contexts.size() << std::endl;
    std::cout << "  总数据流: " << market_data.size() * (1 + 1 + contexts.size()) << " 次处理" << std::endl;
}

/*!
 * \brief 测试交易时段生命周期管理
 * 
 * \details 验证交易时段开始和结束的完整生命周期管理
 */
TEST_F(UftIntegrationTest, TradingSessionLifecycle) {
    uint32_t trading_date = 20241201;
    
    // 交易时段开始
    engine->on_session_begin();
    for (auto& context : contexts) {
        context->on_session_begin(trading_date);
    }
    
    // 验证交易时段开始状态
    EXPECT_TRUE(engine->is_session_active()) << "引擎交易时段应激活";
    for (const auto& context : contexts) {
        EXPECT_EQ(context->get_session_date(), trading_date) << "策略交易日期错误";
    }
    
    // 模拟交易时段内的活动
    engine->on_tick("SHFE.rb2501", 3850.0, 1000);
    data_mgr->handle_push_quote("SHFE.rb2501", 3850.0, 1000);
    
    // 交易时段结束
    engine->on_session_end();
    for (auto& context : contexts) {
        context->on_session_end(trading_date);
    }
    
    // 验证交易时段结束状态
    EXPECT_FALSE(engine->is_session_active()) << "引擎交易时段应未激活";
    for (const auto& context : contexts) {
        EXPECT_EQ(context->get_session_date(), 0U) << "策略交易日期应重置";
    }
    
    std::cout << "交易时段生命周期管理测试通过:" << std::endl;
    std::cout << "  交易日期: " << trading_date << std::endl;
    std::cout << "  参与策略数: " << contexts.size() << std::endl;
    std::cout << "  生命周期状态: 开始->活跃->结束" << std::endl;
}

/*!
 * \brief 测试系统稳定性和错误恢复
 * 
 * \details 验证系统在异常情况下的稳定性和恢复能力
 */
TEST_F(UftIntegrationTest, SystemStabilityAndRecovery) {
    const int stress_operations = 10000;
    std::atomic<int> successful_operations{0};
    std::atomic<int> failed_operations{0};
    
    // 压力测试
    auto start_time = std::chrono::high_resolution_clock::now();
    
    for (int i = 0; i < stress_operations; ++i) {
        try {
            // 模拟各种操作
            if (i % 4 == 0) {
                engine->set_date_time(20241201 + i % 10, 93000 + i % 3600);
                successful_operations++;
            } else if (i % 4 == 1) {
                double price = engine->get_cur_price("SHFE.rb2501");
                if (price > 0) successful_operations++;
                else failed_operations++;
            } else if (i % 4 == 2) {
                engine->on_tick("SHFE.rb2501", 3850.0 + i % 100, 1000 + i % 500);
                successful_operations++;
            } else {
                data_mgr->handle_push_quote("SHFE.rb2501", 3850.0 + i % 100, 1000 + i % 500);
                successful_operations++;
            }
        } catch (...) {
            failed_operations++;
        }
    }
    
    auto end_time = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
    
    // 验证系统稳定性
    EXPECT_GT(successful_operations.load(), stress_operations * 0.95) << "成功操作比例应大于95%";
    EXPECT_LT(failed_operations.load(), stress_operations * 0.05) << "失败操作比例应小于5%";
    EXPECT_LT(duration.count(), 2000) << "压力测试应在2秒内完成";
    
    std::cout << "系统稳定性和错误恢复测试通过:" << std::endl;
    std::cout << "  总操作数: " << stress_operations << std::endl;
    std::cout << "  成功操作: " << successful_operations.load() << std::endl;
    std::cout << "  失败操作: " << failed_operations.load() << std::endl;
    std::cout << "  成功率: " << (double)successful_operations.load() / stress_operations * 100 << "%" << std::endl;
    std::cout << "  总耗时: " << duration.count() << " 毫秒" << std::endl;
}

// 主函数
int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    
    std::cout << "开始执行WtUftEngine及相关UFT模块测试..." << std::endl;
    
    int result = RUN_ALL_TESTS();
    
    if (result == 0) {
        std::cout << "\n=== UFT模块测试总结 ===" << std::endl;
        std::cout << "✓ WtUftEngine基础功能测试" << std::endl;
        std::cout << "✓ WtUftDtMgr数据管理器测试" << std::endl;
        std::cout << "✓ UftStraBaseCtx策略上下文测试" << std::endl;
        std::cout << "✓ UFT高性能处理测试" << std::endl;
        std::cout << "✓ UFT模块集成测试" << std::endl;
        std::cout << "✓ 系统稳定性和错误恢复测试" << std::endl;
        std::cout << "\n所有UFT模块测试通过！" << std::endl;
    } else {
        std::cout << "\n部分UFT模块测试失败，请检查相关实现。" << std::endl;
    }
    
    return result;
} 